/*接收打印从客户端发来的数据报*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define oops(m, x) \
	{              \
		perror(m); \
		exit(x);   \
	}

int make_dgram_server_socket(int);
int get_internet_address(char *, int, int *, struct sockaddr_in *);
void say_who_called(struct sockaddr_in *);

int main(int ac, char *av[])
{
	int port; //使用此端口
	int sock;
	char buf[BUFSIZ]; //接收数据
	size_t msglen;
	struct sockaddr_in saddr; //发送者地址
	socklen_t saddrlen;

	if (ac == 1 || (port = atoi(av[1])) <= 0)
	{
		fprintf(stderr, "usage:接收者号码\n");
		exit(1);
	}

	if ((sock = make_dgram_server_socket(port)) == -1)
		oops("不能make socket", 2);

	saddrlen = sizeof(saddr);
	while ((msglen = recvfrom(sock, buf, BUFSIZ, 0, &saddr, &saddrlen)) > 0)
	{
		buf[msglen] = '\0';
		printf("dgrecv:获得信息:%s\n", buf);
		say_who_called(&saddr);
	}
	return 0;
}

void say_who_called(struct sockaddr_in *addrp)
{
	char host[BUFSIZ];
	int port;

	get_internet_address(host, BUFSIZ, &port, addrp);
	printf("来自：%s:%d\n", host, port);
}
